package duyd.hibernate.training.test;

import java.util.Date;

import javax.persistence.EntityManager;

import org.junit.Test;

import duyd.hibernate.training.domain.Event;
import duyd.hibernate.training.domain.Location;

public class HibernateORM extends HibernateJPA {

	@Test
	public void test() {
		EntityManager entityManager = entityManagerFactory
				.createEntityManager();
		entityManager.getTransaction().begin();
		Event oddMainEvent = new Event("Odd Main Event", new Date());
		Event evenMainEvent = new Event("Even Main Event", new Date());
		Location oddLocation = new Location("Odd Location", "Odd Address");
		Location evenLocation = new Location("Even Location", "Even Address");
		entityManager.persist(oddMainEvent);
        entityManager.persist(evenMainEvent);
        entityManager.persist(evenLocation);
        entityManager.persist(oddLocation);
		for (int i = 0; i < 10; i++) {
			Event event = new Event("Event " + i, new Date());
			if (i % 2 == 0) {
				event.setMainEvent(evenMainEvent);
				evenMainEvent.getSubEvents().add(event);
				evenLocation.getEvents().add(event);
			} else {
				event.setMainEvent(oddMainEvent);
				oddMainEvent.getSubEvents().add(event);
				oddLocation.getEvents().add(event);
			}
		}		
		entityManager.getTransaction().commit();
		entityManager.close();
	}

}
